package HBaseDemo.ReadFile;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

//读取HDFS数据文件
public class RFFHMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> {
/*写出要封装成HBase的io类型*/

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //从HDFS读数据(按行)
        String line = value.toString();
        //按行切割"\t"数据
        String[] fields= line.split("\t");
        //取值
        String rowkey = fields[0];
        String name = fields[1];
        String color = fields[2];

        //初始化rowkey
        ImmutableBytesWritable rowkeyWritable = new ImmutableBytesWritable(Bytes.toBytes(rowkey));

        //初始化Put对象
        Put put = new Put(Bytes.toBytes(rowkey));
        //设置参数(封装name数据)
        put.addColumn(Bytes.toBytes("info"),
                  Bytes.toBytes("name"),
                  Bytes.toBytes(name));
        //设置参数(封装color数据)
        put.addColumn(Bytes.toBytes("info"),
                Bytes.toBytes("color"),
                Bytes.toBytes(color));
        //写出封装好的数据
        context.write(rowkeyWritable, put);

    }
}
